import { OnInit, Component, Output, Input, EventEmitter, OnChanges } from '@angular/core';
import { AcademyDTO } from '../../project/model/icm/icm-base/academy/academy.dto';
import { ProfessionService } from 'src/app/project/service/icm/icm-base/profession/profession.service';
import { ProfessionDTO } from 'src/app/project/model/icm/icm-base/profession/profession.dto';
import { QueryOptions } from 'src/app/project/model/common/query-options';

@Component({
  selector: 'app-profession-select',
  templateUrl: './profession-select.component.html',
  styles: [],
})
export class ProfessionSelectComponent implements OnInit, OnChanges {
  constructor(private professionService: ProfessionService) {}

  @Input()
  academyId: string;

  @Input()
  professionId: string;
  @Output()
  professionIdChange = new EventEmitter<string>();

  /**
   * 专业名称
   */
  @Input()
  professionName: string;

  /**
   * 是否预览
   */
  @Input()
  isPreView: boolean;

  /**
   * 专业下拉
   */
  professionList: Array<ProfessionDTO> = [];

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'id,asc',
  };

  /**
   * 专业下拉加载
   */
  isLoading = false;

  /**
   * 获取专业下拉
   */
  getProfessionDropDownList() {
    this.professionList = [];
    this.professionService.findOnePage(this.queryOptions, null, null, this.academyId).subscribe(data => {
      if (data) {
        this.professionList.forEach(profession => {
          // 删除与原有专业重复的专业
          data.data.splice(
            data.data.findIndex(item => item === profession),
            1,
          );
        });
        this.professionList = [...this.professionList, ...data.data];
      }
    });
    // 非联级下拉，查出目标学院下的专业
    // this.professionService.findAllBySort('showOrder', 'asc').subscribe(data => {
    //   this.isLoading = false;
    //   this.professionList.forEach(profession => {
    //     // 删除与原有专业重复的专业
    //     data.splice(
    //       data.findIndex(item => item === profession),
    //       1,
    //     );
    //   });
    //   this.professionList = [...this.professionList, ...data];
    // });
  }

  /**
   * 弹射数据
   */
  ngModelChange($event: string) {
    this.professionIdChange.emit($event);
  }

  ngOnInit() {
    this.getProfessionDropDownList();
  }

  ngOnChanges() {
    this.getProfessionDropDownList();
  }
}
